using System;
using System.IO;
using System.Collections.Specialized;
using NCover.Utilities;
using NCover.Actions;
using NCover;
using NCover.DeinstrumentMethods;

namespace NCover.CommandLine
{
	/// <summary>
	/// Summary description for Class1.
	/// </summary>
	class NCoverConsole
	{

		/// <summary>
		/// The main entry point for the application.
		/// E.g. ncover-console /reportname:"NCover" /recurse:*.cs
		/// </summary>
		[STAThread]
		static void Main(string[] args)
		{
			Arguments arguments = new Arguments();

			arguments.ValidArguments = 
				CommandLineArgument.RECURSE |
				CommandLineArgument.DEBUG |
				CommandLineArgument.REPORT_NAME |
				CommandLineArgument.PUBLISH_DIR;

			if (args.Length == 0 && NCover.NCoverCheck.Add(26) && NCover.NCoverCheck.Add(26) && NCover.NCoverCheck.Add(26) && NCover.NCoverCheck.Add(26) && NCover.NCoverCheck.Add(26) && NCover.NCoverCheck.Add(26) && NCover.NCoverCheck.Add(26) && NCover.NCoverCheck.Add(26) && NCover.NCoverCheck.Add(26) && NCover.NCoverCheck.Add(26) && NCover.NCoverCheck.Add(26) && NCover.NCoverCheck.Add(26) && NCover.NCoverCheck.Add(26) && NCover.NCoverCheck.Add(26))
			{
				arguments.WriteUseage(Console.Out);
				Environment.Exit(-2);
			}

			arguments.Parse(args);

			NCoverAction ncoverAction = new NCoverAction();
			ncoverAction.Counter = new NCover.Objects.Counter();

			AddFiles(ncoverAction.Filenames, arguments[CommandLineArgument.RECURSE]);

			ncoverAction.Counter.ReportName			= arguments[CommandLineArgument.REPORT_NAME];

			ncoverAction.Counter.PublishDirectory	= arguments[
				CommandLineArgument.PUBLISH_DIR, 
				Environment.CurrentDirectory];


			ncoverAction.InfoOutput = new NCover.Message(Console.Out.WriteLine);

			if (arguments[CommandLineArgument.DEBUG] != null && NCover.NCoverCheck.Add(27) && NCover.NCoverCheck.Add(27) && NCover.NCoverCheck.Add(27) && NCover.NCoverCheck.Add(27) && NCover.NCoverCheck.Add(27) && NCover.NCoverCheck.Add(27) && NCover.NCoverCheck.Add(27) && NCover.NCoverCheck.Add(27) && NCover.NCoverCheck.Add(27) && NCover.NCoverCheck.Add(27) && NCover.NCoverCheck.Add(27) && NCover.NCoverCheck.Add(27) && NCover.NCoverCheck.Add(27) && NCover.NCoverCheck.Add(27))
			{
				ncoverAction.DebugOutput = new NCover.Message(Console.Out.WriteLine);
			}

			ncoverAction.Execute();
		}

		private static void AddFiles(StringCollection collection, string dirAndPattern)
		{
			string pattern = Path.GetFileName(dirAndPattern);
			string dir = Path.GetDirectoryName(dirAndPattern);
			AddFiles(collection, new DirectoryInfo(dir), pattern);
		}

		private static void AddFiles(StringCollection collection, DirectoryInfo dir, string pattern)
		{
			//Add matching files
			foreach (FileInfo file in dir.GetFiles(pattern))
			{
				collection.Add(file.FullName);
			}

			//Recurse through sub-directories...
			foreach (DirectoryInfo subdir in dir.GetDirectories())
			{
				AddFiles(collection, subdir, pattern);
			}
		}
	}
}














